package com.peanut.baby.mvp.expert;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.User;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.expert.intro.ExpertIntroFragment;
import com.peanut.baby.mvp.expert.live.ExpertLiveListFragment;
import com.peanut.baby.mvp.expert.qa.ExpertQaFragment;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.devlibrary.adapter.FragmentTabAdapter;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.TitleLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by anonymouslystudio on 2017/7/19.
 */

public class ExpertProfileActivity extends BaseMvpActivity implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener,
        TitleLayout.OnTitleClickListener,
        ExpertProfileContract.View,View.OnClickListener {

    @BindView(R.id.title)
    TitleLayout title;

    @BindView(R.id.expert_radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.expert_viewpager)
    ViewPager viewpager;

    ArrayList<Fragment> fragments;
    FragmentTabAdapter adapter;
    @BindView(R.id.expert_avatar)
    CircleImageView avatar;
    @BindView(R.id.expert_nick)
    TextView nick;
    @BindView(R.id.expert_fans_count)
    TextView fansCount;
    @BindView(R.id.expert_follow)
    CheckBox follow;
    private ExpertProfilePresenter presenter;
    private String expertId;

    private User user;

    private ExpertIntroFragment introFg;

    /**
     * @param ctx
     * @param expertId
     */
    public static void start(Context ctx, String expertId) {
        ctx.startActivity(new Intent(ctx, ExpertProfileActivity.class).putExtra("expertId", expertId));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expert_profile);
        ButterKnife.bind(this);
        expertId = getIntent().getStringExtra("expertId");
        if (StringUtil.isNullOrEmpty(expertId)) {
            finish();
            return;
        }
        presenter = new ExpertProfilePresenter(this,this);
        initView();

    }

    private void initView() {
        title.setOnTitleClickedListener(this);
        title.setTitle("专家介绍");

        follow.setOnClickListener(this);
        viewpager.setOnPageChangeListener(this);
        radioGroup.setOnCheckedChangeListener(this);

        fragments = new ArrayList<>();
        introFg = ExpertIntroFragment.newInstance();
        fragments.add(introFg);
        fragments.add(ExpertLiveListFragment.newInstance(expertId));
        fragments.add(ExpertQaFragment.newInstance(expertId));
        adapter = new FragmentTabAdapter(getSupportFragmentManager(), fragments);
        viewpager.setAdapter(adapter);

        presenter.getDoctorInfo(expertId);

    }

    private void setCurrent(int currentIndex) {

        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(currentIndex);
        if (!radioButton.isChecked()) {
            radioButton.setChecked(true);
        }

        if (viewpager.getCurrentItem() != currentIndex) {
            viewpager.setCurrentItem(currentIndex);
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setCurrent(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.expert_radio_intro:
                setCurrent(0);
                break;
            case R.id.expert_radio_live:
                setCurrent(1);
                break;
            case R.id.expert_radio_qa:
                setCurrent(2);
                break;
        }
    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onDoctorInfoGet(User user) {
        this.user = user;
        bindUserInfoViews();
        introFg.setUser(user);
    }


    private void bindUserInfoViews() {
        if (user != null) {
            ImageLoader.getInstance().loadImageView(ExpertProfileActivity.this, user.avatar, avatar);
            nick.setText(user.nickname);

            fansCount.setText("粉丝 " + user.fansCount);
            follow.setChecked(user.isConcerned == 1);
            follow.setText(user.isConcerned == 1? "已关注":"关注");
            if(InitManager.getInstance().getUserId().equals(user.userId)){
                follow.setVisibility(View.GONE);
            }else{
                follow.setVisibility(View.VISIBLE);
            }

        }
    }

    @Override
    public void onGetDoctorInfoFailed(String reason) {
        showToast("获取专家信息失败 "+reason);
        finish();
    }

    @Override
    public void onChangeStatusSuccess(int isConcern) {
        dismissProgressDialog();
        user.isConcerned = isConcern;
        bindUserInfoViews();
    }

    @Override
    public void onChangeStatusFailed(String reason) {
        dismissProgressDialog();
        showToast(reason);
        bindUserInfoViews();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.expert_follow:
                if(InitManager.getInstance().getUser() == null){
                    LoginActivity.startForResult(ExpertProfileActivity.this);
                    bindUserInfoViews();
                    return;
                }
                if(StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().userToken)){
                    BindMobileActivity.startForResult(ExpertProfileActivity.this);
                    bindUserInfoViews();
                    return;
                }
                showProgressDialog("请稍候...",false);
                presenter.changeFollowStatus(expertId);
                break;
        }
    }
}
